package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MessageManager;
import com.itheima.app.manager.UserManager;
import com.itheima.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class MessageController {

    @Autowired
    private MessageManager messageManager;

    @GetMapping("/huanxin/user")
    public ResponseEntity findHuanxinUser(){
        //1. 获取线程内userId
        Long userId = UserHolder.get().getId();
        //2. 封装vo
        HuanXinVo vo = new HuanXinVo();
        vo.setUsername("HX"+userId);
        vo.setPassword("123456");
        //3. 返回
        return ResponseEntity.ok(vo);
    }

    //向陌生人发送一条消息
    @PostMapping("/tanhua/strangerQuestions")
    public void strangerQuestions(@RequestBody Map<String,String> param){
        //1. 接收参数
        long jiarenId = Long.parseLong(param.get("userId"));
        String reply = param.get("reply");
        //2.调用manager
        messageManager.strangerQuestions(jiarenId,reply);
    }

   //添加好友
    @PostMapping("/message/contacts")
    public void addContacts(@RequestBody Map<String,Long> param){
        //1. 接收请求参数
        Long friendId = param.get("userId");
        //2.调用manager
        messageManager.addContacts(friendId);
    }

    // 查询好友
    @GetMapping("/messages/contacts")
    public ResponseEntity  findContactVoByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            String keyword
    ){
        //调用manger
        return messageManager.findContactVoByPage(pageNum,pageSize,keyword);
    }

    @Autowired
    private UserManager userManager;

    //根据环信id查询用户头像和昵称
    @GetMapping("/messages/userinfo")
    public ResponseEntity findUserInfo(String huanxinId){
        //1. 处理下环信id
        String userId = huanxinId.replace("HX", "");
        //2. 查询
        return userManager.findUserInfoVo(Long.parseLong(userId));
    }


    // 查询点赞人信息
    @GetMapping("/messages/likes")
    public ResponseEntity likes(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize
    ){
        //指定类型
        Integer commentType=1;
        //调用manager
        return messageManager.findUserCommentVoByPage(commentType,pageNum,pageSize);
    }

    //查看评论人信息
    @GetMapping("/messages/comments")
    public ResponseEntity comments(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize
    ){
        //指定类型
        Integer commentType=2;
        //的manager
        return messageManager.findUserCommentVoByPage(commentType,pageNum,pageSize);
    }

    // 查询喜欢的人信息
    @GetMapping("/messages/loves")
    public ResponseEntity loves(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize
    ){
        //指定类型
        Integer commentType=3;
        //掉用manager
        return messageManager.findUserCommentVoByPage(commentType,pageNum,pageSize);
    }

}
